//
//  NoNetWorking.m
//  Youhuiju
//
//  Created by yutenys on 16/7/13.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "NoNetWorking.h"

#import "UIImage+ColorImage.h"

#define IMG_WIDTH 100.0
#define IMG_HEIGHT 72.0

@interface NoNetworkView ()

@property (assign, nonatomic) CGRect imageFrame;
@property (strong, nonatomic) UIImageView *imageView;

@property (strong, nonatomic) NSMutableArray *images;

@end

@implementation NoNetworkView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageFrame = frame;
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.backgroundColor  = [UIColor whiteColor];
    
    UIButton *retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [retryButton setTitle:@"重新加载" forState:UIControlStateNormal];
    [retryButton setTitleColor:[UIColor colorWithHexString:@"#969696"] forState:UIControlStateNormal];
    retryButton.titleLabel.font = [UIFont systemFontOfSize:14];
    retryButton.layer.borderColor = [UIColor grayColor].CGColor;
    retryButton.layer.borderWidth = 1.0;
    retryButton.layer.cornerRadius = 6.0;
    
    
    [retryButton addTarget:self action:@selector(retry:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:retryButton];
    
    [self addSubview:self.imageView];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(IMG_WIDTH));
        make.height.equalTo(@(IMG_HEIGHT));
        make.bottom.equalTo(retryButton.mas_top).offset(-20*FitH);
        make.centerX.equalTo(self.mas_centerX);
    }];
    
    [retryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.mas_top).offset(252 *FitH);
        make.width.equalTo(@(95));
        make.height.equalTo(@30);
    }];
    
}

- (void)show
{
    self.hidden = NO;
    
    if (!self.imageView.isAnimating) {
        [self.imageView startAnimating];
    }
}

-(void)hidde
{
    if (self.imageView.isAnimating) {
        [self.imageView stopAnimating];
    }
    
    self.hidden = YES;
}


- (void)retry:(UIButton *)sender
{
    DLog(@"重新加载");
    if (self.retryHandler) {
        self.retryHandler();
    }
}

-(NSMutableArray *)images
{
    if (!_images) {
        _images = [NSMutableArray array];
        
        for (NSInteger i = 0; i < 14; i++) {
            
            NSString *name = [NSString stringWithFormat:@"No network%li.png",(long)i];
            
            UIImage *image = LoadImage(name);
            
            [_images addObject:image];
        }
    }
    return _images;
}

- (UIImageView *)imageView
{
    if (!_imageView) {
        
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, IMG_WIDTH, IMG_HEIGHT)];
        self.imageView.animationImages = self.images;
        self.imageView.animationDuration = 3.0;
        
        self.imageView.center = self.center;
        
    }
    return _imageView;
}

@end
